package wechat

import (
	"io/ioutil"

	"go-papers/common/go-wechat"
	"go-papers/common/go-wechat/common"
	"go-papers/tools/config"
)

var (
	Server  wechat.IWeChat
	Wasatch common.CUserInfo
)

type Wechat struct {
	Client wechat.IWeChat
}

func Init() {
	var wechatCert []byte
	var wechatKey []byte
	// 判断是否配置 密钥证书
	if config.GConfig.Wechat.ApiclientCert != "" {
		// "config/wechat/apiclient_cert.pem"
		wechatCert, _ = ioutil.ReadFile(config.GConfig.Wechat.ApiclientCert)

	}
	if config.GConfig.Wechat.ApiclientKey != "" {
		// "config/wechat/apiclient_key.pem"
		wechatKey, _ = ioutil.ReadFile(config.GConfig.Wechat.ApiclientKey)
	}

	Wasatch = common.CUserInfo{
		AppId:           config.GConfig.Wechat.AppId,
		AppSecret:       config.GConfig.Wechat.AppSecret,
		Port:            config.GConfig.Wechat.Port,
		Url:             config.GConfig.Wechat.Url,
		Token:           config.GConfig.Wechat.Token,
		MchId:           config.GConfig.Wechat.MchId,
		MchKey:          config.GConfig.Wechat.MchKey,
		SubMchId:        config.GConfig.Wechat.SubMchId,
		SubMchName:      config.GConfig.Wechat.SubMchName,
		ProfitSharing:   config.GConfig.Wechat.ProfitSharing,
		NotifyUrl:       config.GConfig.Wechat.NotifyUrl,
		RefundNotifyUrl: config.GConfig.Wechat.RefundNotifyUrl,
		ApiclientCert:   wechatCert,
		ApiclientKey:    wechatKey,
	}

	Server = wechat.New(&Wasatch)
}
